Imports Microsoft.SqlServer

Public Class SQL

    Private myVersion As String
    Dim WithEvents backupEvents As SQLDMO.Backup2

    Public ReadOnly Property Version() As String
        Get
            Return myVersion
        End Get
    End Property

    Public Sub New(ByVal Server As String, ByVal User As String, ByVal Password As String, ByVal DataBase As String, ByVal DataBaseDir As String)

        Dim SQLServer As New SQLDMO.SQLServer2

        'Console.WriteLine("")
        'Console.WriteLine("Connecting to: " & Server)

        SQLServer.Connect(Server, User, Password)
        myVersion = SQLServer.VersionString

        'Console.WriteLine("")
        'Console.WriteLine("Connected to: " & vbCrLf & myVersion)

        Dim Bck As New SQLDMO.Backup2

        Bck.Database = SQLServer.Databases.Item(DataBase).Name
        Bck.Files = DataBaseDir & "\" & DataBase & ".bak"
        Bck.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database
        Bck.SQLBackup(SQLServer)

        'Console.WriteLine("Database backed up to: " & DataBaseDir & "\" & DataBase & ".bak")

        SQLServer.DisConnect()

    End Sub

    Private Overloads Sub backupEvents_Complete(ByVal Message As String) Handles backupEvents.Complete
        Console.WriteLine(Message)
    End Sub

    Private Overloads Sub backupEvents_NextMedia(ByVal Message As String) Handles backupEvents.NextMedia
        Console.WriteLine(Message)
    End Sub

    Private Overloads Sub backupEvents_PercentComplete(ByVal Message As String, ByVal Percent As Integer) _
        Handles backupEvents.PercentComplete
        Console.WriteLine("{0} {1}", Message, Percent)
    End Sub



End Class
